import { GlobalFunction } from "./GlobalFunction";

const {ccclass, property} = cc._decorator;

@ccclass
export default class copyTexture extends cc.Component {

    @property(cc.Node)
    targetNode:cc.Node = null;

    private _sp:cc.Sprite = null;
    private texture:cc.RenderTexture = null;
    private spFrame:cc.SpriteFrame = null;

    setTarget(target:cc.Node){
        this.targetNode = target;
    }

    onLoad(){
        this._sp = this.node.getComponent(cc.Sprite);
        if(!this._sp){
            this._sp = this.node.addComponent(cc.Sprite);
        }
        this.texture = new cc.RenderTexture();
        this.spFrame = new cc.SpriteFrame(this.texture);
        this._sp.spriteFrame = this.spFrame;
    }

    update(dt){
        if(this._sp && this.targetNode){
            this.texture.initWithSize(256,256);
            GlobalFunction.captureNode(this.targetNode, this.texture)
        }
    }
}
